#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Author  : Leo.W
@Date    : 2024/2/29 14:08
@File    : url_checker.py
@Software: PyCharm
[说明]
"""
import os

import requests


# python -m pip install requests


def check_file(file_path):
    if os.path.isfile(file_path):
        return True
    else:
        print(f"File {file_path} does not exist!")
        return False


def open_url(full_url):
    try:
        with requests.get(full_url) as response:
            print(response.json())
            return response.status_code == 200
    except Exception as ex:
        print(ex)
        return False


def check_url():
    if (
        check_file("resources/url.txt") is False
        or check_file("resources/file.txt") is False
    ):
        return

    try:
        # 读取url.txt和file.txt文件
        with open("resources/url.txt", "r") as url_file:
            urls = url_file.read().splitlines()

        with open("resources/file.txt", "r") as file_file:
            files = file_file.read().splitlines()

        # 遍历所有域名和文件名组合，发送请求并保存状态码为200的网址
        with open("resources/url200.txt", "w") as output_file:
            output_file.truncate(0)  # 清空内容
            for url in urls:
                for file in files:
                    full_url = url + file
                    if open_url(full_url):
                        output_file.write(f"{full_url} - Status Code: 200\n")

        print("检查完毕，状态码为200的网址已保存到url200.txt文件中。")
    except Exception as e:
        print(e)


if __name__ == "__main__":
    check_url()
